//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.IO;

import com.goldhuman.Common.Conf;
import com.goldhuman.IO.NetIO.NetSession;
import com.goldhuman.IO.NetIO.StreamIO;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;

public class ActiveIO extends com.goldhuman.IO.PollIO {
    boolean closing;
    NetSession assoc_session;

    protected int UpdateEvent() {
        return this.closing ? -1 : 8;
    }

    protected void PollConnect() {
        this.closing = true;
    }

    private ActiveIO(SocketChannel sc, NetSession session) {
        super(sc);
        (this.assoc_session = session).LoadConfig();
        this.closing = false;
        com.goldhuman.IO.PollIO.WakeUp();
    }

    public boolean Close() {
        boolean b = true;

        try {
            SocketChannel sc = (SocketChannel)this.channel;
            if (sc.finishConnect()) {
                b = false;
                register(new StreamIO(sc, (NetSession)this.assoc_session.clone()));
                PollIO.WakeUp();
                return false;
            }
        } catch (Exception var4) {
            var4.printStackTrace();
            System.err.println("assoc_session = " + this.assoc_session.getPeerAddress() + " activeio = " + this);
        }

        try {
            this.assoc_session.OnAbort();
            return true;
        } catch (Exception var3) {
            var3.printStackTrace();
            System.err.println("assoc_session = " + this.assoc_session.getPeerAddress() + " activeio = " + this);
            return b;
        }
    }

    public static ActiveIO Open(NetSession assoc_session) {
        Conf conf = Conf.GetInstance();
        String section = assoc_session.Identification();
        String type = conf.find(section, "type");
        if (type.compareToIgnoreCase("tcp") == 0) {
            try {
                SocketChannel sc = SocketChannel.open();
                sc.configureBlocking(false);
                InetSocketAddress sa = null;

                try {
                    sa = new InetSocketAddress(InetAddress.getByName(conf.find(section, "address")), Integer.parseInt(conf.find(section, "port")));
                } catch (Exception var12) {
                }

                Socket sock = sc.socket();

                try {
                    sock.setReceiveBufferSize(Integer.parseInt(conf.find(section, "so_rcvbuf")));
                } catch (Exception var11) {
                }

                try {
                    sock.setSendBufferSize(Integer.parseInt(conf.find(section, "so_sndbuf")));
                } catch (Exception var10) {
                }

                try {
                    if (Integer.parseInt(conf.find(section, "tcp_nodelay")) != 0) {
                        sock.setTcpNoDelay(true);
                    }
                } catch (Exception var9) {
                }

                SocketAddress peer = assoc_session.OnCheckAddress(sa);
                sc.connect(peer);
                NetSession ns = (NetSession)assoc_session.clone();
                ns.setPeerAddress(peer);
                return (ActiveIO)register(new ActiveIO(sc, ns));
            } catch (Exception var13) {
                var13.printStackTrace();
            }
        }

        return null;
    }
}
